<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/10/24
 * Time: 10:07
 */

namespace app\admin\controller;


use think\Db;

class Consumer extends Base
{
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
    
            // 用户角色
            if(isset($params['user_auth']) && $params['user_auth'] != ''){
                $where['user_auth'] = $params['user_auth'];
            }
            // 活动名称
            if(isset($params['user_username']) && $params['user_username'] != ''){
                $where['user_username'] = ['like', '%' . $params['user_username'] . '%'];
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->where($where)->count();// 获取总条数
            $lists = Db::name('u_user')
                ->field('user_id, user_username, user_image, user_phone, user_gender, user_time, 
                user_auth')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('user_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 设置成核销人员
     * @return \think\response\Json
     */
    public function setHeXiao(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_auth', 2);
        if($res){
            return json(['code' => 200, 'msg' => '设置成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 设置成普通用户
     * @return \think\response\Json
     */
    public function setCommon(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_auth', 1);
        if($res){
            return json(['code' => 200, 'msg' => '设置成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function userDel(){
        $user_id = input('id');
        Db::startTrans();
        try{
            // 删除up_u_user表
            Db::table('up_u_user')->where('user_id', $user_id)->delete();
            // 活动报名表
            Db::table('up_active_member')->where('am_user_id', $user_id)->delete();
            
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}